<template>
  <div class="approval-flow">
    <div class="approval-flow-body">
      <flexbox :class="[isShowBody?'approval-flow_hd':'approval-flow_hd_no']" justify="space-between">
        <div class="approval-flow__hd--left">
          <span class="flow-title" @click="btnBody">审批流信息</span>
          <el-popover ref="popover" v-model="showFlowPopover" :placement="placement" width="300" trigger="click">
            <check-flow :id="recordId" ref="checkFlow" :show="showFlowPopover" :examine-type="examineType"
              @close="showFlowPopover = false" />
            <el-button slot="reference" type="text">查看历史审批流程</el-button>
          </el-popover>
        </div>
      </flexbox>

      <div class="flow-body" v-if="isShowBody">
        <flexbox v-for="(item, index) in examineFlowList" :key="index" class="flow-item" align="stretch"
          justify="flex-start">
          <i :class="getStatusIcon(item.examineStatus)" :style="{
            color: getStatusColor(item.examineStatus)
          }" class="flow-item-img" />
          <div>
            <flexbox class="flow-item-head">
              <!-- <div class="flow-item-des">{{ item.orderId|stepName }}</div> -->
              <div><span>{{ item.name ? item.name + ": " : "" }}</span>{{ getStatusName(item.examineStatus) }}</div>
            </flexbox>
            <flexbox class="flow-item-info" v-for="(subItem, subIndex) in item.userList">
              <div class="flow-item-name">
                <div class="name-icon">in</div>{{ subItem.realname }}
                {{ getStatusName(item.examineStatus) }}
              </div>
              <!-- 审核中 3 当做 待审核 0 处理 -->
              <div style="font-size: 12px;color: #a5adba;margin-top: 4px;"><span>{{ subItem.examineTime }}</span></div>
            </flexbox>
            <div v-if="item.remarks" class="flow-item-content">{{ item.remarks }}
              <div class="flow-item-content-arrow" />
            </div>
          </div>
          <div class="flow-item-line" v-if="index != (examineFlowList.length - 1)" />
        </flexbox>
        <div class="flow-btn-group">
          <el-button v-if="examineInfo.isCheck == 1" type="success" @click="examineHandle(1)">通过</el-button>
          <el-button v-if="examineInfo.isCheck == 1" type="danger" @click="examineHandle(2)">拒绝</el-button>
          <el-button v-if="examineInfo.isRecheck == 1" @click="examineHandle(4)">撤回</el-button>
        </div>
      </div>

      <!-- <examine-handle
      :id="id"
      :show="showExamineHandle"
      :record-id="recordId"
      :examine-type="examineType"
      :detail="examineInfo"
      :status="examineHandleInfo.status"
      @close="showExamineHandle = false"
      @save="examineHandleClick" /> -->
    </div>
  </div>
</template>
<script type="text/javascript">

// import Nzhcn from 'nzh/cn'
import ExamineHandle from './ExamineHandle'
import CheckFlow from './CheckFlow'
import CheckStatusMixin from '@/mixins/CheckStatusMixin'
// import { wayTypeObj } from '@/components/ApprovalFlow/nodeModel'

// 审核信息 config 1 固定 0 自选
export default {
  name: 'ExamineInfo', // 合同审核操作
  components: {
    ExamineHandle,
    CheckFlow
  },
  filters: {
    stepName: function (index) {
      return '第' + 1 + '级'
    }
  },
  mixins: [CheckStatusMixin],
  props: {
    examineType: {
      type: String,
      default: ''
    },
    placement: {
      type: String,
      default: 'bottom'
    },
    id: [String, Number],
    // 审批流id
    recordId: [String, Number],
    ownerUserId: [String, Number]
  },
  data () {
    return {
      loading: false,
      isShowBody:true,

      showFlowPopover: false,
      examineHandleInfo: { status: 1 }, // 1 审核通过 2 审核拒绝 4 已撤回
      showExamineHandle: false // 审核操作
    }
  },
  computed: {
    examineFlowList () {
      return this.examineInfo.examineFlowList ? this.examineInfo.examineFlowList : []
    },
    examineInfo () {
      var data = this.getData();
      return data
    }
  },
  watch: {
    recordId: {
      handler (val) {
        if (val) {
          this.examineInfo = {}
          this.getFlowStepList()
          if (this.$refs.checkFlow) {
            this.$refs.checkFlow.getDetail()
          }
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted () { },
  methods: {
    btnBody(){
      this.isShowBody = !this.isShowBody;
    },
    getData () {
      var data = {
        "label": 1,
        "isRecheck": 1,
        "isCheck": 1,
        "createUser": {
          "userId": "1685860863323926528",
          "img": null,
          "realname": "admin",
          "status": 1,
          "deptId": "1685860863307149312",
          "deptName": "admin",
          "wxUserId": null,
          "outerUserEmail": null
        },
        "examineFlowList": [
          {
            "flowId": "0",
            "examineType": null,
            "rangeType": null,
            "name": null,
            "remarks": null,
            "examineErrorHandling": null,
            "parentLevel": null,
            "roleId": null,
            "type": null,
            "chooseType": null,
            "userList": [
              {
                "outerUserEmail": null,
                "img": null,
                "userId": "1685860863323926528",
                "examineStatus": 6,
                "examineTime": "2023-08-07 14:23:33",
                "realname": "admin"
              }
            ],
            "examineStatus": 6,
            "passFlag": null
          },
          {
            "flowId": "1685860863743356978",
            "examineType": 4,
            "rangeType": null,
            "name": "合同审批流程",
            "remarks": null,
            "examineErrorHandling": 1,
            "parentLevel": null,
            "roleId": null,
            "type": 1,
            "chooseType": null,
            "userList": [
              {
                "outerUserEmail": null,
                "img": null,
                "userStatus": 1,
                "createTime": "2023-08-07 14:23:33",
                "userId": "1685860863323926528",
                "examineStatus": 3,
                "passFlag": null,
                "examineTime": "2023-08-07 14:23:39",
                "remarks": null,
                "realname": "admin"
              }
            ],
            "examineStatus": 3,
            "passFlag": null
          }
        ],
        "examineAdvancedConfigVO": {
          "advancedConfigId": null,
          "examineId": null,
          "nodeHandleType": 1,
          "nodeHandleUser": null,
          "repeatHandleType": 1,
          "rejectHandleType": 2,
          "modifyPermissionType": [
            1,
            2
          ],
          "limitTimeStatus": 0,
          "advancedLimitTimeVO": null
        }
      }
      return JSON.parse(JSON.stringify(data));
    },
    /**
     * 详情
     */
    getFlowStepList () {
      if (!this.recordId) {
        return
      }
      this.loading = true
      examineRecordQueryListAPI({
        recordId: this.recordId,
        ownerUserId: this.ownerUserId
      })
        .then(res => {
          this.loading = false
          const resData = res.data || {}
          this.examineInfo = resData
        })
        .catch(() => {
          this.loading = false
        })
    },

    /**
     * 撤回审核 通过 拒绝
     */
    examineHandle (status) {
      this.examineHandleInfo.status = status
      this.showExamineHandle = true
    },

    /**
     * 审批操作点击
     */
    examineHandleClick (data) {
      this.getFlowStepList()
      if (this.$refs.checkFlow) {
        this.$refs.checkFlow.getDetail()
      }
      this.$emit('on-handle', data)
    },

    /**
     * 获取名称
     */
    getDetailName (data) {
      if (!data.userList || data.userList.length === 0) {
        return 'XX'
      } else if (data.userList.length === 1) {
        return data.userList[0].realname
      } else if (data.examineType === 5) {
        return `${data.userList.length}人${wayTypeObj[1]}`
      } else {
        return `${data.userList.length}人${wayTypeObj[data.type]}`
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.approval-flow {
  position: relative;
  background-color: white;

  &-body{
    border-radius: 4px;
    border: 1px solid #dfe1e6;
    padding-bottom: 8px;
  }

  &_hd {
    position: relative;
    border-radius: 4px;
    padding: 8px 16px;
    border-bottom: 1px solid #dfe1e6;
  }
  &_hd_no {
    position: relative;
    border-radius: 4px;
    padding: 8px 16px 0px 16px;
  }
}


.flow-title {
  margin-right: #{$--interval-base * 3};
  font-size: $--font-size-large;
  font-weight: $--font-weight-medium;
}


.flow-body {
  padding: 8px;
}

.flow-btn-group {
  padding-top: 40px;
  padding-left: 5px;

}

.flow-item {
  position: relative;
  padding-bottom: #{$--interval-base * 2};

  .flow-item-img {
    height: 18px;
    margin-right: $--interval-base;
    font-size: 18px;
    background-color: white;
  }

  .flow-item-head {
    color: $--color-text-secondary;

    .flow-item-des {
      margin-right: 10px;
    }
  }

  .flow-item-info {
    margin-top: $--interval-base;
    display: block;

    .flow-item-name {
      margin-right: $--interval-base;
      display: flex;
      align-items: center;

      .name-icon {
        height: 26px;
        width: 26px;
        line-height: 26px;
        font-size: 12px;
        background: #0052cc;
        border-radius: 50%;
        text-align: center;
        color: #fff;
        margin-right: 8px;
        ;
      }
    }
  }

  .flow-item-content {
    position: relative;
    padding: 8px;
    margin-top: 15px;
    font-size: 12px;
    line-height: 18px;
    color: $--color-text-secondary;
    background-color: $--color-n20;
    border-radius: $--border-radius-base;

    .flow-item-content-arrow {
      position: absolute;
      top: -4px;
      left: 25px;
      width: 8px;
      height: 8px;
      background-color: $--color-n20;
      transform: rotate(45deg);
    }
  }
}

::v-deep .flow-item-img {
  z-index: 1;
}

.flow-item-line {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 8px;
  z-index: 0;
  width: 1px;
  background-color: $--border-color-base;
}
</style>
